package com.yhp.web;

import com.yhp.bean.Users;
import com.yhp.service.UsersService;
import com.yhp.service.impl.GradeServiceImpl;
import com.yhp.service.impl.RoleServiceImpl;
import com.yhp.service.impl.UsersServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/power/user/users"})
public class UsersServlet  extends HttpServlet {
    private UsersService usersService=new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }
        if ("getRole".equals(method)){
            getRole(req,resp);
        }
        if ("add".equals(method)){
            addUser(req,resp);
        }
        if ("edit".equals(method)){
            edit(req,resp);
        }
        if ("updata".equals(method)){
            updata(req,resp);
        }
    }

    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Users users =new Users();
        users.setLoginName(req.getParameter("loginName"));
        users.setPassWord(req.getParameter("passWord"));
        users.setRealName(req.getParameter("realName"));
        users.setSex(Integer.parseInt(req.getParameter("sex")));
        users.setRoleId(Integer.parseInt(req.getParameter("roleId")));
        users.setEmail(req.getParameter("email"));
        users.setPhone(req.getParameter("phone"));
        users.setAddress(req.getParameter("address"));
        users.setCardId(req.getParameter("cardId"));
        int zt = new  UsersServiceImpl().insert(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(zt>0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users?method=select';</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/power/user/users?method=getRole';</script>");
        }
    }

    private void getRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //调取service取出role的信息
        List roleList = new RoleServiceImpl().getRoleList(1,10);
        req.setAttribute("rl",roleList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }

    private void updata(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service对该数据进行修改
        Users users = new Users();
        String userid = req.getParameter("userid");
        users.setLoginName(req.getParameter("loginName"));
        users.setPassWord(req.getParameter("passWord"));
        users.setRealName(req.getParameter("realName"));
        users.setSex(Integer.parseInt(req.getParameter("sex")));
        users.setRoleId(Integer.parseInt(req.getParameter("roleId")));
        users.setEmail(req.getParameter("email"));
        users.setPhone(req.getParameter("phone"));
        users.setAddress(req.getParameter("address"));
        users.setCardId(req.getParameter("cardId"));
        users.setUserId(Integer.parseInt(userid));
        //跳转页面
        int zt = new  UsersServiceImpl().update(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(zt>0){
            writer.println("<script>alert('修改成功');location.href='/power/user/users?method=select';</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/power/user/users?method=edit&userid="+userid+"';</script>");
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userid = req.getParameter("userid");
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users user = usersService.getUser(Integer.parseInt(userid));
        RoleServiceImpl roleService = new RoleServiceImpl();
        List g = roleService.getRoleList(1,10);
        req.setAttribute("rl",g);
        req.setAttribute("user",user);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
       //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(usersList);
        pageUtil.setTotalPages(pageUtil.getTotalPages());
        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);



    }


}
